package com.marquisx.tzdice;



import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;

import com.marquisx.common.utils.TZUtils;
import com.marquisx.tzdice.model.DiceActionManager;
import com.marquisx.tzdice.preference.NumberPreference;
import com.marquisx.tzdice.preference.UserProfilePreference;

public class Setting extends PreferenceActivity implements 
					OnPreferenceChangeListener, 
					OnPreferenceClickListener
{
	private static final String TAG = "TZDice:setting";
	
	private DiceActionManager actionManager;
	
	// Option names and default values
	private static final String OPT_USERPROFILE = "User_Profile";
	private static final int OPT_USERPROFILE_DEF = Constants.USE_RPROFILE_PIC_ID_DEF;
	
	private static final String OPT_USERNAME = "Username" ;
	private static final String OPT_USERNAME_DEF = "username";
	private static final String OPT_SHAKE = "Shake";
	private static final boolean OPT_SHAKE_DEF = true;
	private static final String OPT_SHAKE_SENSITIVITY = "Shake_Sensitivity";
	private static final String OPT_SHAKE_SENSITIVITY_DEF = "3E-7f";
	private static final String OPT_VIBRATE = "Vibrate";
	private static final boolean OPT_VIBRATE_DEF = false;
	private static final String OPT_ANIMATION = "Animation";
	private static final boolean OPT_ANIMATION_DEF = true;
	private static final String OPT_SOUND_EFFECT = "Sound_Effect";
	private static final boolean OPT_SOUND_EFFECT_DEF = false;
	private static final String OPT_DICE = "Default_Dice";
	private static final String OPT_DICE_DEF = Constants.OPT_DICE_DEF+"";
	private static final String OPT_LOCALE = "Locale";
	private static final String OPT_LOCALE_DEF = "en_US";
	
	private static final String OPT_TIMES_MAXIMUM = "Times_Maximum";
	private static final int OPT_TIMES_MAXIMUM_DEF = Constants.OPT_TIMES_MAX_DEF;
	private static final String OPT_OFFSET_MAXIMUM = "Offset_Maximum";
	private static final int OPT_OFFSET_MAXIMUM_DEF = Constants.OPT_OFFSET_MAX_DEF;
	private static final String OPT_OFFSET_MINIMUM = "Offset_Minimum";
	private static final int OPT_OFFSET_MINIMUM_DEF = Constants.OPT_OFFSET_MIN_DEF;
	private static final String OPT_TEXT_SIZE = "Text_Size";
	private static final String OPT_TEXT_SIZE_DEF = Constants.OPT_TEXT_SIZE_DEF+"";
	
	private static final String OPT_ACTION_SETTING = "Action_Setting";
	private static final String OPT_ACTION_CREATE = "Action_Create";
	private static final String OPT_ACTION_CLEAN_ALL = "Action_Clean_All";
	private static final String OPT_ACTION_RESTORE = "Action_Restore";
	
	private static final String OPT_DESKTOP_BACKGROUND_COLOR = "Desktop_Background_Color";
	private static final int OPT_DESKTOP_BACKGROUND_COLOR_DEF = Constants.OPT_BG_COLOR_DEF;
	
	private static final String OPT_DESKTOP_BACKGROUND_PIC = "Background_Picture";
	private static final int OPT_DESKTOP_BACKGROUND_PIC_DEF = Constants.OPT_BG_PIC_DEF;
	
	private ListPreference defaultDiceLP;
	private ListPreference localeLP;
	private Preference actionViewListPref;
	private Preference actionCreatePref;
	private Preference actionCleanAllPref;
	private Preference actionRestorePref;
	private UserProfilePreference userProfilePref;
	private NumberPreference timesMaxPref;
	private NumberPreference offsetMaxPref;
	private NumberPreference offsetMinPref;
	
	public static int getUserProfilePicId(Context context)
	{
		try
		{
			return PreferenceManager.getDefaultSharedPreferences(context)
							.getInt(OPT_USERPROFILE, OPT_USERPROFILE_DEF);
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_USERPROFILE, OPT_USERPROFILE_DEF)
				.commit();
			return OPT_USERPROFILE_DEF;
		}
		
	}
	
	public static void setUserProfilePicId(Context context, int userProfilePicId)
	{
		PreferenceManager.getDefaultSharedPreferences(context)
		 			.edit()
		 			.putInt(OPT_USERPROFILE, userProfilePicId)
		 			.commit();
	}
	
	public static String getUsername(Context context)
	{
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(OPT_USERNAME, OPT_USERNAME_DEF);
	}
	
	public static boolean getShake(Context context)
	{
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_SHAKE, OPT_SHAKE_DEF);
	}
	
	public static void setShake(Context context, boolean isShake)
	{
		PreferenceManager.getDefaultSharedPreferences(context)
						 .edit()
						 .putBoolean(OPT_SHAKE, isShake)
						 .commit();
	}
	
	public static float getShakeSensitivity(Context context)
	{
		return Float.parseFloat(PreferenceManager.getDefaultSharedPreferences(context)
				.getString(OPT_SHAKE_SENSITIVITY, OPT_SHAKE_SENSITIVITY_DEF));
	}
	
	public static boolean getVibrate(Context context)
	{
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_VIBRATE, OPT_VIBRATE_DEF);
	}
	
	public static boolean getAnimation(Context context)
	{
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_ANIMATION, OPT_ANIMATION_DEF);
	}
	
	public static boolean getSoundEffect(Context context)
	{
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_SOUND_EFFECT, OPT_SOUND_EFFECT_DEF);
	}
	
	public static int getDefaultDice(Context context)
	{
		try
		{
			return Integer.parseInt( PreferenceManager.getDefaultSharedPreferences(context)
									.getString(OPT_DICE, OPT_DICE_DEF));
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_DICE, Integer.parseInt(OPT_DICE_DEF))
				.commit();
			return Integer.parseInt(OPT_DICE_DEF);
		}
	}
	
	public static int getTimesMaximum(Context context)
	{
		try
		{
			return PreferenceManager.getDefaultSharedPreferences(context)
										.getInt(OPT_TIMES_MAXIMUM, OPT_TIMES_MAXIMUM_DEF);
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_TIMES_MAXIMUM, OPT_TIMES_MAXIMUM_DEF)
				.commit();
			return OPT_TIMES_MAXIMUM_DEF;
		}
	}
	
	public static int getOffsetMaximum(Context context)
	{
		try
		{
			return PreferenceManager.getDefaultSharedPreferences(context)
										.getInt(OPT_OFFSET_MAXIMUM, OPT_OFFSET_MAXIMUM_DEF);
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_OFFSET_MAXIMUM, OPT_OFFSET_MAXIMUM_DEF)
				.commit();
			return OPT_OFFSET_MAXIMUM_DEF;
		}
	}
	
	public static int getOffsetMinmum(Context context)
	{
		try
		{
			return PreferenceManager.getDefaultSharedPreferences(context)
									.getInt(OPT_OFFSET_MINIMUM, OPT_OFFSET_MINIMUM_DEF);
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_OFFSET_MINIMUM, OPT_OFFSET_MINIMUM_DEF)
				.commit();
			return OPT_OFFSET_MINIMUM_DEF;
		}
	}
	
	public static String getLocal(Context context)
	{
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(OPT_LOCALE, OPT_LOCALE_DEF);
	}
	
	public static float getTextSize(Context context)
	{
		try
		{
			return Float.parseFloat(PreferenceManager.getDefaultSharedPreferences(context)
									.getString(OPT_TEXT_SIZE, OPT_TEXT_SIZE_DEF));
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putFloat(OPT_TEXT_SIZE, Float.parseFloat(OPT_TEXT_SIZE_DEF))
				.commit();
			return Float.parseFloat(OPT_TEXT_SIZE_DEF);
		}
	}
	
	public static int getBackgroundColor(Context context)
	{
		try
		{
			return PreferenceManager.getDefaultSharedPreferences(context)
							.getInt(OPT_DESKTOP_BACKGROUND_COLOR, OPT_DESKTOP_BACKGROUND_COLOR_DEF);
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_DESKTOP_BACKGROUND_COLOR, OPT_DESKTOP_BACKGROUND_COLOR_DEF)
				.commit();
			return OPT_DESKTOP_BACKGROUND_COLOR_DEF;
		}
	}
	
	public static void setBackgroundColor(Context context, int color)
	{
		PreferenceManager.getDefaultSharedPreferences(context)
		 			.edit()
		 			.putInt(OPT_DESKTOP_BACKGROUND_COLOR, color)
		 			.commit();
	}
	
	public static int getBackgroundPictureId(Context context)
	{
		try
		{
			return PreferenceManager.getDefaultSharedPreferences(context)
							.getInt(OPT_DESKTOP_BACKGROUND_PIC, OPT_DESKTOP_BACKGROUND_PIC_DEF);
		}
		catch(ClassCastException e)
		{
			Log.e(TAG, e.getMessage());
			PreferenceManager.getDefaultSharedPreferences(context)
				.edit()
				.putInt(OPT_DESKTOP_BACKGROUND_PIC, OPT_DESKTOP_BACKGROUND_PIC_DEF)
				.commit();
			return OPT_DESKTOP_BACKGROUND_PIC_DEF;
		}
	}
	
	public static void setBackgroundPictureId(Context context, int pictureId)
	{
		PreferenceManager.getDefaultSharedPreferences(context)
		 			.edit()
		 			.putInt(OPT_DESKTOP_BACKGROUND_PIC, pictureId)
		 			.commit();
	}
	
	/** Called when the activity is first created.*/
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		actionManager = DiceActionManager.getInstance(this);
		
		Log.v(TAG, "onCreate");
		addPreferencesFromResource(R.xml.setting);
		findViews();
		prepareViews();
		setListensers();
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		//this.onCreate(null);
		userProfilePref.refresh();
		setTitle(getText(R.string.app_name) + " " + getText(R.string.app_version_name) + " - " + getText(R.string.setting_title));
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		actionManager.closeDB();
	}
	
	private void findViews()
	{
		defaultDiceLP = (ListPreference)findPreference(OPT_DICE);
		localeLP = (ListPreference)findPreference(OPT_LOCALE);
		actionViewListPref = (Preference)findPreference(OPT_ACTION_SETTING);
		actionCreatePref = (Preference)findPreference(OPT_ACTION_CREATE);
		actionCleanAllPref = (Preference)findPreference(OPT_ACTION_CLEAN_ALL);
		actionRestorePref = (Preference)findPreference(OPT_ACTION_RESTORE);
		
		timesMaxPref = (NumberPreference)findPreference(OPT_TIMES_MAXIMUM);
		offsetMaxPref = (NumberPreference)findPreference(OPT_OFFSET_MAXIMUM);
		offsetMinPref = (NumberPreference)findPreference(OPT_OFFSET_MINIMUM);
		
		userProfilePref = (UserProfilePreference)findPreference(OPT_USERPROFILE);
	}
	
	private void prepareViews()
	{
		setTitle(getText(R.string.app_name) + " " + getText(R.string.app_version_name) + " - " + getText(R.string.setting_title));
		changeDefaultDiceListPreference( Integer.parseInt(defaultDiceLP.getValue()));
		//localeLP.setDefaultValue(getLocal(this));
		
		timesMaxPref.setMax(Constants.OPT_TIMES_UPPER_LIMIT);
		timesMaxPref.setMin(Constants.OPT_TIMES_LOWER_LIMIT);
		
		offsetMaxPref.setMax(Constants.OPT_OFFSET_MAX_UPPER_LIMIT);
		offsetMaxPref.setMin(Constants.OPT_OFFSET_MAX_LOWER_LIMIT);
		
		offsetMinPref.setMax(Constants.OPT_OFFSET_MIN_UPPER_LIMIT);
		offsetMinPref.setMin(Constants.OPT_OFFSET_MIN_LOWER_LIMIT);
	}
	
	private void setListensers()
	{
		defaultDiceLP.setOnPreferenceChangeListener(this);
		localeLP.setOnPreferenceChangeListener(this);
		actionViewListPref.setOnPreferenceClickListener(this);
		actionCreatePref.setOnPreferenceClickListener(this);
		actionCleanAllPref.setOnPreferenceClickListener(this);
		actionRestorePref.setOnPreferenceClickListener(this);
	}
	
	private void changeDefaultDiceListPreference( int currentDice )
	{
		defaultDiceLP.setTitle(getText(R.string.default_dice_title)+ ": D" + currentDice);
		defaultDiceLP.setDialogTitle(getText(R.string.default_dice_title)+ ": D" + currentDice);
	}
	
	private void changeLocal( String localeCode )
	{
		Log.i(TAG, "locale code is " + localeCode);
		
		String currentLocale = getLocal(this);//localeLP.getValue();
		if( currentLocale.equals(localeCode) ) return;
		
		localeLP.setValue(localeCode);
		
		TZUtils.changeLocale(this, localeCode);
        //refresh the view
        this.onCreate(null);
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue)
	{
		String key = preference.getKey();
		Log.i(TAG,newValue.toString());
		if( key.equals(OPT_DICE) )
		{
			changeDefaultDiceListPreference( Integer.parseInt((String)newValue) );
			return true;
		}
		else if( key.equals(OPT_LOCALE) )
		{
			changeLocal((String)newValue);
			return true;
		}
		return false;
	}

	@Override
	public boolean onPreferenceClick(Preference preference)
	{
		String key = preference.getKey();
		if( OPT_ACTION_SETTING.equals(key) )
		{
			startActivity(new Intent(this, ActionSetting.class));
		}
		else if( OPT_ACTION_CREATE.equals(key) )
		{
			ActionSetting.doAdd(this, actionManager);
		}
		else if( OPT_ACTION_CLEAN_ALL.equals(key))
		{
			ActionSetting.doClearAll(this, actionManager);
		}
		else if( OPT_ACTION_RESTORE.equals(key) )
		{
			ActionSetting.doRestore(this, actionManager);
		}
		
		return false;
	}

}
